package Demo1;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-17
 * Time: 16:20
 */
public class BinaryTree {

    static class TreeNode{//节点

        public char val;//数值域
        public TreeNode left;//左孩子节点
        public TreeNode right;//右孩子节点

        public TreeNode(char data){//构造方法
            this.val = data;
        }
    }

    public TreeNode root;//根节点

    public void createTree(){//创建一颗二叉树
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        this.root = A;
        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        E.right = H;
    }

    //morris序
    public  void morris(TreeNode root){

        TreeNode cur = root;
        while(cur != null){
            System.out.print(cur.val + " ");
            //没有左孩子
            if(cur.left == null){
                cur = cur.right;
                continue;
            }
            TreeNode rightMost = cur.left;
            while(rightMost.right != null && rightMost.right != cur){
                rightMost = rightMost.right;
            }
            if(rightMost.right == null){
                rightMost.right = cur;
                cur = cur.left;
            } else{
                rightMost.right = null;
                cur = cur.right;
            }
        }
    }

    public static void morris2(TreeNode head) {
        if (head == null) {
            return;
        }
        TreeNode cur = head; //cur指针一开始在头结点
        TreeNode mostRight = null; //左树上的最右节点
        while (cur != null) { //cur为空就停止
            mostRight = cur.left; //指向左孩子
            if (mostRight != null) { //左孩子不为空，即左树不为空
                //找到最右节点
                //因为有人为改的情况，所以当最右节点的右指针为空或者指向cur的时候停止
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                //此时的mostRight是cur左树上的最右节点
                if (mostRight.right == null) {
                    mostRight.right = cur; //人为修改最右节点右指针的指向
                    cur = cur.left; //cur向左移动
                    continue;
                } else { //mostRight的右指针指向cur
                    mostRight.right = null; //使mostRight的右指针指向空
                }
            }
            cur = cur.right; //cur向右移动
        }
    }

    public static void preOrderMorris(TreeNode head){
        if (head == null) {
            return;
        }
        TreeNode cur = head; //cur指针一开始在头结点
        TreeNode mostRight = null; //左树上的最右节点

        while (cur != null) { //cur为空就停止
            mostRight = cur.left; //指向左孩子
            if (mostRight != null) { //左孩子不为空，即左树不为空
                //找到最右节点
                //因为有人为改的情况，所以当最右节点的右指针为空或者指向cur的时候停止
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                //此时的mostRight是cur左树上的最右节点
                if (mostRight.right == null) {
                    System.out.print(cur.val + " ");
                    mostRight.right = cur; //人为修改最右节点右指针的指向
                    cur = cur.left; //cur向左移动
                    continue;
                } else { //mostRight的右指针指向cur
                    mostRight.right = null; //使mostRight的右指针指向空
                    cur =cur.right;
                    continue;
                }
            }
            System.out.print(cur.val + " ");
            cur = cur.right; //cur向右移动
        }
    }

    public static void inOrderMorris(TreeNode head){
        if (head == null) {
            return;
        }
        TreeNode cur = head; //cur指针一开始在头结点
        TreeNode mostRight = null; //左树上的最右节点

        while (cur != null) { //cur为空就停止
            mostRight = cur.left; //指向左孩子
            if (mostRight != null) { //左孩子不为空，即左树不为空
                //找到最右节点
                //因为有人为改的情况，所以当最右节点的右指针为空或者指向cur的时候停止
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                //此时的mostRight是cur左树上的最右节点
                if (mostRight.right == null) {

                    mostRight.right = cur; //人为修改最右节点右指针的指向
                    cur = cur.left; //cur向左移动
                    continue;
                } else { //mostRight的右指针指向cur
                    mostRight.right = null; //使mostRight的右指针指向空
                }
            }
            System.out.print(cur.val + " ");
            cur = cur.right; //cur向右移动
        }
    }

    //打印
    public void preOrder(TreeNode root){//前序遍历
        if(root == null) return ;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){//中序遍历
        if(root == null) return ;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void postorder(TreeNode root){//后序遍历
        if(root == null) return ;
        postorder(root.left);
        postorder(root.right);
        System.out.print(root.val + " ");
    }

    //节点个数
    public static int nodeNumber = 0;//节点数
    public void nodeNum1(TreeNode root){//遍历每个节点
        if(root == null) return ;
        nodeNumber++;
        nodeNum1(root.left);
        nodeNum1(root.right);
    }

    //节点个数
    public int nodeNumber2(TreeNode root){//子问题法
        if(root == null) return 0;
        return nodeNumber2(root.left) + nodeNumber2(root.right) + 1;
    }

    //叶子节点个数
    public static int leafNumber = 0;
    public void leafNum1(TreeNode root){//遍历二叉树
        if(root == null) return ;
        if(root.right == null && root.left == null){
            leafNumber++;
            return;
        }
        leafNum1(root.left);
        leafNum1(root.right);
    }

    //叶子节点个数
    public int leafNum2(TreeNode root){//子问题
        if(root == null) return 0;
        if(root.left == null && root.right == null) return 1;
        return leafNum2(root.left) + leafNum2(root.right);
    }

    //第K层节点数目
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null) return 0;
        if(k == 1) return 1;
        return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root.right,k - 1);
    }

    //二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null) return 0;
        return Math.max(getHeight(root.left),getHeight(root.right)) + 1;
    }

    // 检测值为 value的元素是否存在
    public TreeNode find(TreeNode root, char val){
        if(root == null) return null;
        if(root.val == val) return root;

        TreeNode ret = find(root.left,val);
        if(ret != null) return ret;

        ret = find(root.right,val);
        if(ret != null) return ret;

        return null;
    }

    //判断两颗数是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //1.判断结构是否一样
        if(p == null && q != null || p != null && q == null) return false;
        if(p == null && q == null) return true;
        //2.判断值是否一样
        if(p.val != q.val) return false;
        //3.判断左数和右数是否一样
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root == null) return;
        //创建队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null) queue.offer(cur.left);
            if(cur.right != null) queue.offer(cur.right);
        }
        System.out.println();
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if(root == null) return true;

        //创建队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            TreeNode cur  = queue.poll();
            if(cur == null) break;
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while(!queue.isEmpty()){
            TreeNode ret = queue.peek();
            if(ret != null){
                return false;
            }else{
                queue.poll();
            }
        }
        return true;
    }

    //非递归前序遍历
    public void preOrder2(TreeNode root){
        if(root == null) return ;
        //创建栈存储节点
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            //遍历左节点
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode tmp = stack.peek();
            if (tmp.right != null) {
                cur = tmp.right;
                stack.pop();
            }else{
                stack.pop();
            }

        }
        System.out.println();
    }

    //非递归中序遍历
    public void inOrder2(TreeNode root){
        if(root == null) return ;
        //创建栈存储节点
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            //遍历左节点
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = stack.peek();
            System.out.print(tmp.val + " ");
            if (tmp.right != null) {
                cur = tmp.right;
                stack.pop();
            }else{
                stack.pop();
            }
        }
        System.out.println();
    }

    //非递归后序遍历
    public void postorder2(TreeNode root){
        if(root == null) return ;
        //创建栈存储节点
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()) {
            //遍历左节点
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = stack.peek();
            if (prev == tmp.right || tmp.right == null) {//打印D有两种情况：
                                                         // 1.右边为null
                                                         // 2.D右边不为null,但是D的右边已经打印完成
                System.out.print(tmp.val + " ");
                prev = tmp;
                stack.pop();
            } else {
                cur = tmp.right;
            }
        }
        System.out.println();
    }
}
